//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    imgUrls: [
      '/pages/images/banner_1.jpg'
    ],
  
    interval: 5000,
    duration: 1000,
  },
  
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
    
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        'lang': 'zh_CN',
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        },
        fail:err=>{
          wx.reLaunch({
            url: '/pages/authWX/auth',
          })
        }
      })
    }
    this.checkAddress();
  },
  //拨打电话
  calltelphone:function(e){
    wx.makePhoneCall({
      phoneNumber: '1340000' //仅为示例，并非真实的电话号码
    })
  },
//查询地址
  checkAddress:function(){
 var her = this;
    var longitude;
    var latitude;
    wx.getLocation({
      success: function (res) {
        longitude = res.longitude
        latitude = res.latitude
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&get_poi=1&key=Q4QBZ-DAYWD-HBJ4Q-PASSY-UBAW3-SMFT3',
          method: "get",
          header: { 'Content-Type': 'application/json' },
          success: function (ops) {
            console.log(ops)
            app.globalData.detailAddress = ops.data.result.address;
            her.setData({
              formatted_address: ops.data.result.address,
            })
          }
        })
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
